﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.IO;
using System.Text;
using System.Xml;
using ConfigGen.Utilities.Xml;
using NUnit.Framework;

namespace ConfigGen.Utilities.Tests.Xml
{
    [TestFixture]
    public class XmlReaderToWriterCopierTests
    {
        [Test]
        public void Copy001_SingleChildElement()
        {
            const string xml = @"<root><child /></root>";
            
            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy002_TextContents()
        {
            const string xml = @"<root>SomeText</root>";
            
            string output= RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy003_Attributes()
        {
            const string xml = @"<root attribute1=""value1"" attribute2=""value2"" />";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy004_AttributesAndChildElement()
        {
            const string xml = @"<root attribute1=""value1"" attribute2=""value2""><child /></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy005_InhibitAttributeWriting()
        {
            const string xml = @"<root attribute1=""value1"" attribute2=""value2""><child /></root>";
            const string expected = @"<root><child /></root>";
            string output = RunCopyTest(xml, (n, r, w) => n != XmlNodeType.Attribute);

            Assert.AreEqual(expected, output);
        }

        [Test]
        public void Copy007_CDATA()
        {
            const string xml = @"<root><![CDATA[SomeText]]></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy008_Comments()
        {
            const string xml = @"<root><!-- This is my comment --></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy009_ProcessingInstruction()
        {
            const string xml = @"<root><?processing instruction?></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy010_Whitespace()
        {
            const string xml = @"<root>  </root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy011_Whitespace()
        {
            const string xml = @"<root>  <child />  </root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy012_ElementWithNamespace()
        {
            const string xml = @"<root xmlns:ns=""http://test/""><ns:child /></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        [Test]
        public void Copy013_AttributeWithNamespace()
        {
            const string xml = @"<root xmlns:ns=""http://test/""><child ns:attr1=""value1"" /></root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }


        /// <summary>
        /// Regression test: escaped entities should be written out as-is, not "unescaped". 
        /// Issue http://configgen.codeplex.com/workitem/9 - "PrettyPrint xml formatter is erroneously unescaping escaped entities."
        /// </summary>
        [Test]
        public void Copy014_EntitiesInAttributesAreWrittenOutVerbatim()
        {
            const string xml = @"<root attribute1=""&lt;triangular braces&gt;"" />";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        /// <summary>
        /// Regression test: escaped entities should be written out as-is, not "unescaped"
        /// Issue http://configgen.codeplex.com/workitem/9 - "PrettyPrint xml formatter is erroneously unescaping escaped entities."
        /// </summary>
        [Test]
        public void Copy015_EntitiesAsTextContentAreWrittenOutVerbatim()
        {
            const string xml = @"<root>&lt;triangular braces&gt;</root>";

            string output = RunCopyTest(xml, (n, r, w) => true);

            Assert.AreEqual(xml, output);
        }

        public string RunCopyTest(string input, Func<XmlNodeType, XmlReader, XmlWriter, bool> onCopyCallback)
        {
            using (var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(input)))
            using (var reader = XmlReader.Create(xmlStream))
            {
                var copier = new XmlReaderToWriterCopier();

                using (var xmlOutput = new StringWriter())
                using (var writer = new XmlTextWriter(xmlOutput))
                {
                    copier.Copy(reader, writer, onCopyCallback);
                    return xmlOutput.ToString();
                }
            }
        }
    }
}
